<?php


namespace app\admin\controller;


use app\admin\service\RequestService;
use app\model\SetSystem;
use app\model\SetSystemCate;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Cache;
use think\model\Relation;

class SetSystemController extends AController {

    public function typeOptions(){
        return jsonSuccess([
            'options' => [
                ['title' => '单行文本', 'id' => 'text'],
                ['title' => '多行文本', 'id' => 'textarea'],
                ['title' => '数字', 'id' => 'number'],
                ['title' => '单选', 'id' => 'radio'],
                ['title' => '上传图片', 'id' => 'image']
            ]
        ]);
    }

    /**
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws DataNotFoundException
     */
    public function index(RequestService $curdService): \think\response\Json
    {
        $list = $curdService->index(new SetSystem(), ['cate'],[],false)->each(function($v){
            $v->cateTitle = getRelValue($v->cate, 'title');
        });

        return jsonSuccess([
            'items' => $list,
        ]);
    }

    public function add(RequestService $curdService){
        return $curdService->add(new SetSystem());
    }

    public function edit(RequestService $curdService){
        if (request()->isPost()){
            return $curdService->edit(new SetSystem());
        }else{
            $id = request()->param('id');
            $info = SetSystem::find($id);
            return jsonSuccess([
                'info' => $info
            ]);
        }

    }

    public function set(){
        if (request()->isPost()){

            // 去除上传文件字段
            $data = request()->except(['file']);
            foreach ($data as $k => $v){
                SetSystem::update(['value' => $v], ['key' =>$k]);
            }

            // 清除缓存
            Cache::tag('system')->clear();

            return jsonSuccess();

        }else{

            $system = $this->system;

            $cates = SetSystemCate::with([
                'children' => function (Relation $query) {
                    $query->with('systems')->withAttr('systems.param', function ($value) {
                        $param = str_replace("'", '"', $value);
                        $arr = json_decode($param, true);
                        if (!$arr) {
                            $arr = [];
                        }
                        // 转为vue端统一格式
                        $paramArr = [];
                        foreach ($arr as $k=>$v){
                            $paramArr[] = ['id' => $k, 'title'=> $v];
                        }
                        return $paramArr;
                    })->withField(['id', 'title', 'pid']);
                },
                'systems'
            ])
                ->withAttr('systems.param', function ($value) {

                    $param = str_replace("'", '"', $value);
                    $arr = json_decode($param, true);
                    if (!$arr) {
                        $arr = [];
                    }
                    // 转为vue端统一格式
                    $paramArr = [];
                    foreach ($arr as $k=>$v){
                        $paramArr[] = ['id' => $k, 'title'=> $v];
                    }
                    return $paramArr;

                })
                ->where('status', 1)
                ->where('pid', 0)->select();

            return jsonSuccess([
                'system' => $system,
                'cates' => $cates
            ]);

        }
    }

    protected function formatList($list){
        $list = $list->each(function ($v) {
            $v->cateTitle = getRelValue($v->cate, 'title');
        });
        return $list;
    }


}
